<?php
namespace Admin\Model;
use Think\Model;
class YujiaMemberModel extends Model 
{
	protected $insertFields = array('name','telphone','sex','birthday','level','status','code_number','intro','account','start_time','end_time','form','company_id');
	protected $updateFields = array('id','name','telphone','sex','birthday','level','status','code_number','intro','account','start_time','end_time','form');
	protected $_validate = array(
		array('name', 'require', '会员名称不能为空！', 1, 'regex', 3),
		array('name', '1,255', '会员名称的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('telphone', 'require', '联系电话不能为空！', 1, 'regex', 3),
		array('telphone', '1,255', '联系电话的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('sex', 'require', '性别不能为空！', 1, 'regex', 3),
		array('sex', '1,255', '性别的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('birthday', 'require', '生日不能为空！', 1, 'regex', 3),
		array('birthday', '1,255', '生日的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('level', 'require', '会员级别不能为空！', 1, 'regex', 3),
		array('level', '1,255', '会员级别的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('status', 'require', '会员状态不能为空！', 1, 'regex', 3),
		array('status', '1,255', '会员状态的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('code_number', 'require', '卡号不能为空！', 1, 'regex', 3),
		array('code_number', 'number', '卡号必须是一个整数！', 1, 'regex', 3),
		array('intro', 'require', '介绍人不能为空！', 1, 'regex', 3),
		array('intro', '1,255', '介绍人的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('account', 'require', '账户余额不能为空！', 1, 'regex', 3),
		array('account', '1,255', '账户余额的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('start_time', 'require', '开始日期不能为空！', 1, 'regex', 3),
		array('end_time', 'require', '结束日期不能为空！', 1, 'regex', 3),
		array('form', 'require', '来源不能为空！', 1, 'regex', 3),
		array('form', '1,255', '来源的值最长不能超过 255 个字符！', 1, 'length', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($name = I('get.name'))
			$where['name'] = array('like', "%$name%");
		if($telphone = I('get.telphone'))
			$where['telphone'] = array('like', "%$telphone%");
		$start_timefrom = I('get.start_timefrom');
		$start_timeto = I('get.start_timeto');
		if($start_timefrom && $start_timeto)
			$where['start_time'] = array('between', array(strtotime("$start_timefrom 00:00:00"), strtotime("$start_timeto 23:59:59")));
		elseif($start_timefrom)
			$where['start_time'] = array('egt', strtotime("$start_timefrom 00:00:00"));
		elseif($start_timeto)
			$where['start_time'] = array('elt', strtotime("$start_timeto 23:59:59"));
		$end_timefrom = I('get.end_timefrom');
		$end_timeto = I('get.end_timeto');
		if($end_timefrom && $end_timeto)
			$where['end_time'] = array('between', array(strtotime("$end_timefrom 00:00:00"), strtotime("$end_timeto 23:59:59")));
		elseif($end_timefrom)
			$where['end_time'] = array('egt', strtotime("$end_timefrom 00:00:00"));
		elseif($end_timeto)
			$where['end_time'] = array('elt', strtotime("$end_timeto 23:59:59"));
		if($form = I('get.form'))
			$where['form'] = array('like', "%$form%");
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = session('admin.company_id');
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
        $data['company_id'] = session('admin.company_id');
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}